package com.buba.matter.controller;

import com.buba.matter.pojo.Info;
import com.buba.matter.pojo.Price;
import com.buba.matter.service.PriceService;
import com.buba.system.pojo.Page;
import com.buba.util.GenerateKey;
import com.buba.util.MyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Price")
public class PriceController {
    private PriceService priceService;

    /**
     * 查询货物价格信息
     * @param map
     * @return
     */
    @RequestMapping("/selPriceTable")
    public Page selPriceTable(@RequestParam Map<String, Object> map){
        Page<Price> page = new Page<>(Integer.parseInt(map.get("pageNow").toString()));
        map.put("page", page);
        String itemsName = map.get("itemsName").toString();
        List<Price> list = null;
        // 没有条件查询
        if ("".equals(itemsName) ) {
            //查询货物价格总条数
            page.computePageTotal(priceService.selPriceCount());
            //查询所有货物价格表
            list = priceService.selPriceTable(page);
        }else { // 携带查询条件
            page.computePageTotal(priceService.selCount(map));
            list = priceService.selAllPrice(map);
        }
        //把list装入page类
        page.setList(list);
        return page;
    }
    /**
     * 添加货物价格信息
     * @RequestParam
     * @return
     */
    @RequestMapping("/addPrice")
    public Map<String,Object> addPrice(@RequestParam Map<String, Object> map){
        //指定格式开头加上当前的事件作为id 调用时间戳类
        String itemsNo = GenerateKey.getKey("MP");
        map.put("itemsNo",itemsNo);
        //1、添加客户信息 返回添加的条数
        Integer count = priceService.addPrice(map);
        if(count>0){
            map.put("message","添加成功");
        }else {
            map.put("message","添加失败");
        }
        return map;
    }
    /**
     * 删除货物价格信息
     * @param
     * @return
     */
    @RequestMapping("/delPriceAll")
    public Map<String,Object> delPriceAll(String ids){
        Map<String,Object> map = new HashMap<>();
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        Integer delPrice = priceService.delPriceAll(list);
        if (delPrice != null){
            map.put("message","删除"+delPrice+"条数据");
        }else{
            map.put("message","删除失败");
        }
        return map;

    }
    /**
     * 修改货物价格信息
     * @param map
     * @return
     */
    @RequestMapping("/updPrice")
    public Map<String, Object> updPrice(@RequestParam Map<String, Object> map,String[] job) {
        Integer updCount = priceService.updPrice(map);
        return map;
    }

    /**
     * 导出excel方法
     * @param response
     * @throws Exception
     */
    @RequestMapping("/importCustomerExcel")
    public void importCustomerExcel(HttpServletResponse response) throws Exception {
        Map<String,Object> map = new HashMap<>();
        //查询客户信息
        List<Price> matterPrice = priceService.selMatterPriceByCondition(map);
        new MyExcel<Price>().exportExcel("物质价格表",matterPrice,Price.class,response);
    }

    /**
     * 查询id
     * @return
     */
    @RequestMapping("/selMatterPriceItemsNo")
    public Map<String, Object> selMatterPriceItemsNo(){
        Map<String, Object> map = new HashMap<>();
        List<Price> list = priceService.selMatterPriceItemsNo(map);
        map.put("list",list);
        return map;
    }


    @Autowired
    public void setPriceService(PriceService priceService) {
        this.priceService = priceService;
    }
}
